package uk.co.q3c.deplan.server.unit.domain.util;

import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import uk.co.q3c.deplan.client.domain.dag.Vertex;

/**
 * @see Vertex
 * @author David Sowerby 27 Jul 2010
 * 
 */
@Test
public class Vertex_UT {

	Vertex<String> v1;
	Vertex<String> v2;
	Vertex<String> v3;
	Vertex<String> v4;
	Vertex<String> v5;
	Vertex<String> v6;

	@BeforeMethod
	protected void beforeMethod() throws Exception {
		v1 = new Vertex<String>("k1");
		v2 = new Vertex<String>("k2");
		v3 = new Vertex<String>("k3");
		v4 = new Vertex<String>("k4");
		v5 = new Vertex<String>("k5");
		v6 = new Vertex<String>("k6");
	}

	public void hasParent() {
		Assert.assertFalse(v1.hasParent("k2"));
		v1.addEdgeFrom(v2);
		Assert.assertTrue(v1.hasParent("k2"));
		Assert.assertFalse(v1.hasParent("k3"));
	}

	public void hasChild() {
		Assert.assertFalse(v1.hasChild("k2"));
		v1.addEdgeTo(v2);
		Assert.assertTrue(v1.hasChild("k2"));
		Assert.assertFalse(v1.hasChild("k3"));
	}

	public void childOrderUpDown() {

		v1.addEdgeTo(v2);
		v1.addEdgeTo(v3);
		v1.addEdgeTo(v4);
		Assert.assertEquals(v1.getChildren()
				.size(), 3);
		Assert.assertEquals(v1.getChildren()
				.indexOf(v2), 0);
		Assert.assertEquals(v1.getChildren()
				.indexOf(v4), 2);

		// promote k3 above k2
		boolean changeMade = v1.childOrderUp("k3");
		Assert.assertEquals(v1.getChildren()
				.indexOf(v2), 1);
		Assert.assertEquals(v1.getChildren()
				.indexOf(v3), 0);
		Assert.assertTrue(changeMade);

		// push k3 down again
		changeMade = v1.childOrderDown("k3");
		Assert.assertEquals(v1.getChildren()
				.indexOf(v2), 0);
		Assert.assertEquals(v1.getChildren()
				.indexOf(v4), 2);
		Assert.assertTrue(changeMade);

		// edge case k2 already at top, does nothing
		changeMade = v1.childOrderUp("k2");
		Assert.assertEquals(v1.getChildren()
				.indexOf(v2), 0);
		Assert.assertEquals(v1.getChildren()
				.indexOf(v4), 2);
		Assert.assertFalse(changeMade);

		// edge case k4 already at bottom, does nothing
		changeMade = v1.childOrderDown("k4");
		Assert.assertEquals(v1.getChildren()
				.indexOf(v2), 0);
		Assert.assertEquals(v1.getChildren()
				.indexOf(v4), 2);
		Assert.assertFalse(changeMade);

		// child not found, just ignore
		// edge case k4 already at bottom, does nothing
		changeMade = v1.childOrderDown("k5");
		Assert.assertFalse(changeMade);
		// edge case k4 already at bottom, does nothing
		changeMade = v1.childOrderUp("k5");
		Assert.assertFalse(changeMade);
		Assert.assertEquals(v1.getChildren()
				.indexOf(v2), 0);
		Assert.assertEquals(v1.getChildren()
				.indexOf(v4), 2);

	}

}
